package mrusanov.ui.battle.actions

import mrusanov.view._
import mrusanov.ui.battle.MiniArenaComponent

class PlayerSpriteAttackAction(arena:MiniArenaComponent, attacker:PlayerSprite, defender:PlayerSprite,
                               successfulAttack:Boolean, defenderDied:Boolean) extends TimeTakingAction {
  private var initialized = false

  def update(time:Int) {
    if (!initialized) {
      init()
      initialized = true
      return
    }

    attacker.update(time)
    defender.update(time)
    arena.repaint()

    if (attacker.state.isStanding) {
      if (defenderDied) {
        if (defender.state != Death && defender.state != NotRender) {
          defender.state = Death
        }

        if (defender.state == NotRender) {
          isCompleted = true
        }

      } else {
        defender.state = Stand
        isCompleted = true
      }
    }

  }

  private def init() {
    attacker.state_=(if (shouldAttackUp) AttackUp
      else AttackDown, Some(successfulAttack))

    defender.state_=(Defence, Some(successfulAttack))
  }

  private def shouldAttackUp = defender.y < attacker.y

}
